package com.ssm.helong.shop.controller;


import com.ssm.helong.shop.common.CommonValuesEnum;
import com.ssm.helong.shop.common.ServiceResultEnum;
import com.ssm.helong.shop.pojo.Login;
import com.ssm.helong.shop.service.UserService;
import com.ssm.helong.shop.util.NumberUtil;
import com.ssm.helong.shop.util.RedisUtils;
import com.ssm.helong.shop.util.Result;
import com.ssm.helong.shop.util.ResultGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    RedisUtils redisUtils = RedisUtils.getRedisUtil();

    /***
     * @param login 登陆接口
     * @return: com.ssm.helong.shop.util.Result
     * @Author: helong.sun
     * @Date: 2021/4/24
     */
    @PostMapping("/login")
    @ResponseBody
    public Result login(@RequestBody Login login) {
        String userLoginName = login.getUserLoginName();
        String userLoginPassword = login.getUserLoginPassword();
        String userPhone = login.getUserPhone();
        String code = login.getCode();
        String choice = login.getChoice();
        if (choice.equals("1") ){//账号密码登陆
            //登录名是否为空
            if (StringUtils.isEmpty(userLoginName)) {
                return ResultGenerator.genFailResult(ServiceResultEnum.LOGIN_NAME_NULL.getResult());
            }
            //登陆密码是否为空
            if (StringUtils.isEmpty(userLoginPassword)) {
                return ResultGenerator.genFailResult(ServiceResultEnum.LOGIN_PASSWORD_NULL.getResult());
            }
            //用户登陆
            String user = userService.passwordLogin(userLoginName, userLoginPassword);
            if (userLoginName.equals(user)) {
                //生成一个随机数作为token的唯一键
                String token = UUID.randomUUID().toString().replace("-", "").toLowerCase();
                redisUtils.set(token, user);
                return ResultGenerator.genSuccessResult();
            }
            return ResultGenerator.genFailResult(ServiceResultEnum.LOGIN_PASSWORD_ERROR.getResult());
        }
        //手机登陆
        if (choice.equals("2")) {
            //手机号是否为空
            if (NumberUtil.isPhone(userPhone)) {
                return ResultGenerator.genFailResult(ServiceResultEnum.LOGIN_PHONE_NULL.getResult());
            }
            //验证码是否为空或不匹配
            String code1 = redisUtils.get(userPhone);
            if (StringUtils.isEmpty(code) || !code.equals(code1)) {
                return ResultGenerator.genFailResult(ServiceResultEnum.LOGIN_VERIFY_CODE_ERROR.getResult());
            }
            String loginResult = userService.phoneLogin(userPhone, code);
            if (ServiceResultEnum.SUCCESS.getResult().equals(loginResult)) {
                String token = UUID.randomUUID().toString().replace("-", "").toLowerCase();
                redisUtils.set(token, loginResult);
                return ResultGenerator.genSuccessResult();
            }
            return ResultGenerator.genFailResult(ServiceResultEnum.LOGIN_VERIFY_CODE_ERROR1.getResult());
        }
        return ResultGenerator.genFailResult("参数异常");
    }

    /***
     * 用户注册
     * userLoginName
     * @param userLoginPassword
     * @param userPhone
     * @return: com.ssm.helong.shop.util.Result
     * @Author: helong.sun
     * @Date: 2021/4/24
     */
    @PostMapping("/register")
    @ResponseBody
    public Result register(String userLoginName, String userLoginPassword, String userPhone) {
        //登录名是否为空
        if (StringUtils.isEmpty(userLoginName)) {
            return ResultGenerator.genFailResult(ServiceResultEnum.LOGIN_NAME_NULL.getResult());
        }
        //登陆密码是否为空
        if (StringUtils.isEmpty(userLoginPassword)) {
            return ResultGenerator.genFailResult(ServiceResultEnum.LOGIN_PASSWORD_NULL.getResult());
        }
        //手机号是否为空
        if (NumberUtil.isPhone(userPhone)) {
            return ResultGenerator.genFailResult(ServiceResultEnum.LOGIN_PHONE_NULL.getResult());
        }
        String registerResult = userService.register(userLoginName, userLoginPassword, userPhone);
        //注册成功
        if (ServiceResultEnum.SUCCESS.getResult().equals(registerResult)) {
            return ResultGenerator.genSuccessResult();
        }
        return ResultGenerator.genFailResult(registerResult);
    }
}
